home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
182_01
/
edit.c
< prev
next >
Wrap
Text File
|
1990-07-30
|
25KB
|
625 lines
/* EDIT.C - Screen Editor
by Bill Kinnersley
MSU Physics Dept
January 1982
EDIT has many of the conveniences of WordStar, but is a much smaller and
faster program. The program is presently limited to editing files of less
than 44K total length, and lines shorter than 80 characters. */
#include <stdioa.h>
#include <iolib.asm>
#include <call.asm>
#define ESC 27
#define BELL 07
#define DC1 17
#define DC3 19
#define EOFCHAR 26
char *topmem; /* pointer to top of memory */
char *topfile; /* pointer to top of file */
char *botfile; /* pointer to bottom of file */
char *topscr; /* pointer to top of screen */
char *botscr; /* pointer to bottom of screen */
char *cursor; /* pointer to current cursor position in file */
char *null; /* null pointer*/
char *mark; /* temporary pointer */
int lines; /* line # where botscr is (1 <= lines <=25) */
int csr_col; /* current cursor column # */
int want_col; /* cursor column desired */
int csr_line; /* current cursor line # */
int next_char; /* next input char, input during screen output*/
int ch; /* current character input from keyboard */
int flag; /* true if cursor==topscr */
int itop; /* topfile as an integer */
int ibot; /* botfile as an integer */
char bufin[80];
char fid[20]; /* name of file to edit */
char backup[20]; /* name of backup file */
int file; /* file descriptor # used by C */
int new; /* true if file was new */
int i; /* number of records written to disk */
main(argc,argv)
int argc; /* number of arguments in command line */
int argv[];
{
topmem = CCAVAIL() - 4096; /* get size of available memory */
botfile = topfile = botscr = topscr = cursor = CCALLOC(topmem);
topmem = botfile + topmem; /* point to top of available memory */
null = "";
next_char = 0;
term_init();
if (argc == 1) /* no command tail */
{ fputs("\nFILE? : ",stderr); /* ask for fid */
gets(fid);
}
else strcpy(fid,argv[1]); /* fid from command line */
fid[14] = 0; /* trim to 14 chars */
new = - swapin(fid,topfile); /* try to read in */
clear_screen();
csr_line = csr_col = want_col = 0;
if (new)
{ fputs("NEW FILE\n\n",stderr); /* must be new file*/
lines = 3;
*botfile = EOFCHAR;
csr_line = 2;
}
else
{ while (*botfile != EOFCHAR) botfile++; /* look for botfile*/
lines = 1;
setbot();
putscr(topfile);
home();
}
while(TRUE)
{ ch = get_conin(); /* wait for console input */
if (ch == ESC ) ch = escape(); /* convert escape sequence */
if (((ch >= ' ') && (ch < 127)) || (ch == '\t') || (ch == '\r'))
ins_character();
else if (special()) /* process special characters */
return; /* quit */
}
}
ins_character()
/* insert character c at current cursor position */
{ disable_keyboard();
e_eol();
ibot = ++botfile; itop = ++cursor;
++botscr;
movmem(cursor-1,cursor,ibot-itop+1);
*(cursor-1) = ch;
if (ch == '\n')
{where();
csr_col = want_col = 0;
if (csr_line == 23)
{botscr = cursor;
lines = 25;
printbot();
}
else
{putch('\n');
setbot();
ins_line();
putline(cursor);
csr_line++;
put_cursor(csr_line,csr_col);
}
}
else
{putch(ch);
if (ch != '\t') csr_col++;
else csr_col = ((csr_col>>3)+1)<<3;
want_col = csr_col;
putline(cursor);
position();
}
enable_keyboard();
}
special()
/* procedure to process special characters */
{ switch (ch) {
/* cursor right */ case 'D'-64 :if (cursor == botfile) break;
else if ((ch=*cursor++)=='\n')
{printbot(); csr_col=0;}
else if (ch=='\t')
{putch('\t');
csr_col = ((csr_col>>3)+1)<<3;}
else {cup_right(); csr_col++;}
want_col = csr_col;
break;
/* cursor left */ case 'S'-64 :
case 'H'-64 : flag = (cursor==topscr);
if (cursor==topfile) break;
else if ((ch=*--cursor)=='\n')
{reverse_index();
if (flag)
{topscr=startline(cursor);
setbot();
putch('\r');
putline(topscr);}
position();
}
else if (ch=='\t') position();
else {putch('\b'); csr_col--;}
want_col = csr_col;
break;
/* cursor up */ case 'E'-64 :
if ((mark=startline(cursor))==topfile) break;
cursor = startline(mark-1);
disable_keyboard();
reverse_index();
where();
if (mark==topscr) {topscr = cursor;
setbot();
cup_home();
putline(topscr);}
set_cursor();
enable_keyboard();
break;
/* cursor down */ case 'X'-64 :
case 'J'-64 : if ((mark = next(cursor))==null) break;
cursor = mark;
printbot();
where();
set_cursor();
break;
/* up screenful */ case 'R'-64 : if (topscr==topfile) break;
disable_keyboard();
where();
botscr = topscr;
settop();
newscr();
enable_keyboard();
break;
/* down screenful */ case 'C'-64 : if (botscr==botfile) break;
disable_keyboard();
topscr = botscr;
setbot();
newscr();
enable_keyboard();
break;
/* top file */ case 'T'-64 : if (topscr==topfile) break;